package com.zzty.daoimpl;

import com.zzty.common.DBUtil;
import com.zzty.common.DBUtilPool;
import com.zzty.common.Ser;
import com.zzty.dao.TeamExamDao;
import com.zzty.entity.HistoryGroup;
import com.zzty.entity.TeamExam;

import sun.security.pkcs11.Secmod;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TeamExamDaoImpl implements TeamExamDao {
    public Integer getIdByDate(String date){
        Integer id=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select id from teamExam where date=?");
            pst.setString(1,date);
            ResultSet rs=pst.executeQuery();
            while (rs.next()){
                id=rs.getInt("id");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }


    @Override
    public List getQuestionIdDateByCompareDate(String newDate) {
        List<TeamExam> teamExams = null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst = cn.prepareStatement("select * from teamExam where date<?");
            pst.setString(1, "date");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                if (teamExams == null) {
                    teamExams = new ArrayList<TeamExam>();
                }
                TeamExam teamExam = new TeamExam();
                teamExam.setQuestionId(rs.getInt("questionId"));
                teamExam.setDate(rs.getDate("date"));
                teamExams.add(teamExam);
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("22221111 " + teamExams.size());
        for (int i = 0; i < teamExams.size(); i++) {
            System.out.println("1111 " + teamExams.get(i).getDate());
            System.out.println("2222 "+teamExams.get(i).getQuestionId());
        }
        return teamExams;
    }

    @Override
    public Date getDateById(Integer id) {
        Date date=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement ps=cn.prepareStatement("select date from teamExam where id=?");
            ps.setInt(1,id);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                date=rs.getDate("date");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    @Override
    public String getQuestionIdById(Integer id) {
        String questionIds=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from teamExam where id=?");
            pst.setInt(1,id);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                questionIds=rs.getString("questionId");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return questionIds;
    }


    public boolean insertQuestionId(String questionId, Integer classId,String date){
        boolean result=false;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("insert into teamExam(questionId,classId,date) values(?,?,?)");
            pst.setString(1,questionId);
            pst.setInt(2,classId);
            pst.setString(3,date);
            Integer count=pst.executeUpdate();
            if(count==1){
                result=true;
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public List getTeamExamToTable() {
        List <TeamExam> teamExams=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from teamExam");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                if(teamExams==null){
                    teamExams=new ArrayList<TeamExam>();
                }
                TeamExam teamExam=new TeamExam();
                teamExam.setClassId(rs.getInt("classId"));
                teamExam.setDate(rs.getDate("date"));
                teamExam.setQuestionId(rs.getInt("questionId"));
                teamExam.setWinnerId1(rs.getInt("winnerId1"));
                teamExam.setWinnerId2(rs.getInt("winnerId2"));
                teamExams.add(teamExam);
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return teamExams;
    }

    public boolean checkDate(String date) {
        boolean result=true;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from teamExam");
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if(date.equals(sdf.format(rs.getDate("date")))){
                    result=false;
                }
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public String getQuestionIdByDate(String date,Integer classId) {
        String questionIds=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select * from teamExam where Date=? and classId=?");
            pst.setString(1,date);
            pst.setInt(2,classId);
            ResultSet rs=pst.executeQuery();
            while(rs.next()){
                questionIds=rs.getString("questionId");
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return questionIds;
    }

    public boolean setWinnersByDate(String date,Integer id1,Integer id2){
        boolean result=false;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("update teamExam set winnerId1=?,winnerId2=? where date=?");
            pst.setInt(1,id1);
            pst.setInt(2,id2);
            pst.setString(3,date);
            Integer count=0;
            count=pst.executeUpdate();
            if(count>0){
                result=true;
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public List getWinnerIdByDate(String date){
        List<Integer> winnerIds=null;
        try {
            Connection cn=DBUtilPool.getConnection();
            PreparedStatement pst=
                    cn.prepareStatement("select winnerId1,winnerId2 from teamExam where date=?");
            pst.setString(1,date);
            ResultSet rs=pst.executeQuery();
            while (rs.next()){
                winnerIds=new ArrayList<Integer>();
                winnerIds.add(rs.getInt("winnerId1"));
                winnerIds.add(rs.getInt("winnerId2"));
            }
            DBUtilPool.releaseConnection(cn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return winnerIds;
    }
}
